<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="关注状态" prop="subscribe">
      <el-input v-model="dataForm.subscribe" placeholder="关注状态"></el-input>
    </el-form-item>
    <el-form-item label="OPENID" prop="openid">
      <el-input v-model="dataForm.openid" placeholder="OPENID"></el-input>
    </el-form-item>
    <el-form-item label="昵称" prop="nickname">
      <el-input v-model="dataForm.nickname" placeholder="昵称"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="sex">
      <el-input v-model="dataForm.sex" placeholder="性别"></el-input>
    </el-form-item>
    <el-form-item label="国家" prop="country">
      <el-input v-model="dataForm.country" placeholder="国家"></el-input>
    </el-form-item>
    <el-form-item label="省份" prop="province">
      <el-input v-model="dataForm.province" placeholder="省份"></el-input>
    </el-form-item>
    <el-form-item label="城市" prop="city">
      <el-input v-model="dataForm.city" placeholder="城市"></el-input>
    </el-form-item>
    <el-form-item label="用户头像" prop="headimgurl">
        <template slot-scope="scope">
          <img  :src="dataForm.headimgurl" alt="" style="width: 50px;height: 50px">
        </template>
    </el-form-item>
    <el-form-item label="关注时间" prop="subscribeTime">
      <el-input v-model="dataForm.subscribeTime" placeholder="关注时间"></el-input>
    </el-form-item>
    <el-form-item label="粉丝的备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="粉丝的备注"></el-input>
    </el-form-item>
    <el-form-item label="标签ID列表" prop="tagidList">
      <el-input v-model="dataForm.tagidList" placeholder="标签ID列表"></el-input>
    </el-form-item>
    <!-- <el-form-item label="分组ID" prop="groupid">
      <el-input v-model="dataForm.groupid" placeholder="分组ID"></el-input>
    </el-form-item> -->
    <el-form-item label="扫码场景" prop="qrScene">
      <el-input v-model="dataForm.qrScene" placeholder="扫码场景"></el-input>
    </el-form-item>
    <el-form-item label="扫码场景描述" prop="qrSceneStr">
      <el-input v-model="dataForm.qrSceneStr" placeholder="扫码场景描述"></el-input>
    </el-form-item>
    <el-form-item label="渠道来源" prop="subscribeScene">
      <el-input v-model="dataForm.subscribeScene" placeholder="渠道来源"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          subscribe: '',
          openid: '',
          nickname: '',
          sex: '',
          language: '',
          country: '',
          province: '',
          city: '',
          headimgurl: '',
          subscribeTime: '',
          unionid: '',
          remark: '',
          tagidList: '',
          groupid: '',
          qrScene: '',
          qrSceneStr: '',
          subscribeScene: '',
          account: '',
          createTime: ''
        },
        dataRule: {
          subscribe: [
            { required: true, message: '关注状态不能为空', trigger: 'blur' }
          ],
          openid: [
            { required: true, message: 'OPENID不能为空', trigger: 'blur' }
          ],
          nickname: [
            { required: true, message: '昵称不能为空', trigger: 'blur' }
          ],
          sex: [
            { required: true, message: '性别不能为空', trigger: 'blur' }
          ],
          language: [
            { required: true, message: '语言不能为空', trigger: 'blur' }
          ],
          country: [
            { required: true, message: '国家不能为空', trigger: 'blur' }
          ],
          province: [
            { required: true, message: '省份不能为空', trigger: 'blur' }
          ],
          city: [
            { required: true, message: '城市不能为空', trigger: 'blur' }
          ],
          headimgurl: [
            { required: true, message: '用户头像不能为空', trigger: 'blur' }
          ],
          subscribeTime: [
            { required: true, message: '关注时间不能为空', trigger: 'blur' }
          ],
          unionid: [
            { required: true, message: '用户头像不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '粉丝的备注不能为空', trigger: 'blur' }
          ],
          tagidList: [
            { required: true, message: '标签ID列表不能为空', trigger: 'blur' }
          ],
          groupid: [
            { required: true, message: '分组ID不能为空', trigger: 'blur' }
          ],
          qrScene: [
            { required: true, message: '扫码场景不能为空', trigger: 'blur' }
          ],
          qrSceneStr: [
            { required: true, message: '扫码场景描述不能为空', trigger: 'blur' }
          ],
          subscribeScene: [
            { required: true, message: '渠道来源不能为空', trigger: 'blur' }
          ],
          account: [
            { required: true, message: '原始id不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/cms/accountfans/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.subscribe = data.data.subscribe
                this.dataForm.openid = data.data.openid
                this.dataForm.nickname = data.data.nickname
                this.dataForm.sex = data.data.sex
                this.dataForm.language = data.data.language
                this.dataForm.country = data.data.country
                this.dataForm.province = data.data.province
                this.dataForm.city = data.data.city
                this.dataForm.headimgurl = data.data.headimgurl
                this.dataForm.subscribeTime = data.data.subscribeTime
                this.dataForm.unionid = data.data.unionid
                this.dataForm.remark = data.data.remark
                this.dataForm.tagidList = data.data.tagidList
                this.dataForm.groupid = data.data.groupid
                this.dataForm.qrScene = data.data.qrScene
                this.dataForm.qrSceneStr = data.data.qrSceneStr
                this.dataForm.subscribeScene = data.data.subscribeScene
                this.dataForm.account = data.data.account
                this.dataForm.createTime = data.data.createTime
                this.dataForm.subscribe = data.accountfans.subscribe
                this.dataForm.openid = data.accountfans.openid
                this.dataForm.nickname = data.accountfans.nickname
                this.dataForm.gender = data.accountfans.gender
                this.dataForm.language = data.accountfans.language
                this.dataForm.country = data.accountfans.country
                this.dataForm.province = data.accountfans.province
                this.dataForm.city = data.accountfans.city
                this.dataForm.headimgurl = data.accountfans.headimgurl
                this.dataForm.subscribeTime = data.accountfans.subscribeTime
                this.dataForm.unionid = data.accountfans.unionid
                this.dataForm.remark = data.accountfans.remark
                this.dataForm.tagidList = data.accountfans.tagidList
                this.dataForm.groupid = data.accountfans.groupid
                this.dataForm.qrScene = data.accountfans.qrScene
                this.dataForm.qrSceneStr = data.accountfans.qrSceneStr
                this.dataForm.subscribeScene = data.accountfans.subscribeScene
                this.dataForm.account = data.accountfans.account
                this.dataForm.createTime = data.accountfans.createTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/cms/accountfans/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'subscribe': this.dataForm.subscribe,
                'openid': this.dataForm.openid,
                'nickname': this.dataForm.nickname,
                'sex': this.dataForm.gender,
                'language': this.dataForm.language,
                'country': this.dataForm.country,
                'province': this.dataForm.province,
                'city': this.dataForm.city,
                'headimgurl': this.dataForm.headimgurl,
                'subscribeTime': this.dataForm.subscribeTime,
                'unionid': this.dataForm.unionid,
                'remark': this.dataForm.remark,
                'tagidList': this.dataForm.tagidList,
                'groupid': this.dataForm.groupid,
                'qrScene': this.dataForm.qrScene,
                'qrSceneStr': this.dataForm.qrSceneStr,
                'subscribeScene': this.dataForm.subscribeScene,
                'account': this.dataForm.account,
                'createTime': this.dataForm.createTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
